<?php
/**
 * PaperController
 *
 * PHP Version 5.4
 *
 * @author liuxuTJ <liuxuphp@icesmart.cn>
 */
namespace Admin\Controller;

use Admin\Service\PermissionService;
use Admin\Service\UserService;
use Model\PaperModel;
use Model\QuestionModel;
use Model\QuestionTagModel;
use Think\Controller;

/**
 * Class PaperController
 */
class PaperController extends BaseController
{

    public function __construct()
    {
        parent::__construct();
        $userService = new UserService();
        $userInfo = $userService->validateLogin();   //验证是否登录

        $permissionService = new PermissionService();
        $permission = $permissionService->validate($userInfo['userId'], [
            'paperAdd' => '/'.CONTROLLER_NAME.'/paperAdd',
            'paperEdit' => '/'.CONTROLLER_NAME.'/paperEdit',
            'paperDel' => '/'.CONTROLLER_NAME.'/paperDel'
        ]);

        $menu = $permissionService->menu($userInfo['userId']);

        $this->assign('menu', $menu);
        $this->assign('permission', $permission);
        $this->assign('userInfo', $userInfo);
    }

    /**
     * 试卷列表
     */
    public function index()
    {
        $questionTagModel = new QuestionTagModel();
        $questionModel = new QuestionModel();
        $tagList = $questionTagModel->select();

        foreach($tagList as $k=>$v){
            $tagList[$k]['question'] = $questionModel->where(" `tagId` = {$v['id']}")->select();
        }
        $this->assign("tagList",$tagList);

        $model = new PaperModel();
        $page = $this->page($model, null);
        $show = $page->show();
        $limit[] = $page->firstRow;
        $limit[] = $page->listRows;
        $list = $model->findByFilter(null, ['id' => 'DESC'], $limit);
        $this->assign('list', $list);
        $this->assign("page", $show);
        $this->display();
    }

    /**
     * 添加试卷
     */
    public function paperAdd(){
        $data['name'] = I("post.name");
        $data['question_time']= I("post.question_time");
        $data['question_time'] = intval($data['question_time']);
        $data['titles'] = I("post.titles");
        $data['titles'] = implode(",",$data['titles']);
        $data['titles'] = ",".$data['titles'];
        if(empty($data['name'])){
            $this->error("标题不可为空","",1);
        }
        if(empty($data['question_time']) || $data['question_time'] == 0){
            $this->error("考试时间至少1分钟","",1);
        }
        $model = new PaperModel();
        $where['name'] = $data['name'];
        $findPaper = $model->where($where)->find();

        if(!empty($findPaper)){
            $this->error("此试卷名称已被占用！",'',1);
        }
        $add = $model->add($data);
        if ($add) {
            $this->success('添加成功', '/Admin/Paper', 1);
        } else {
            $this->error('添加失败','',1);
        }
    }

    /**
     * 删除试卷
     */
    public function paperDel()
    {

        $id = intval(I("get.id"));
        if (empty($id) || $id == 0) {
            $this->error("非法参数！",'',1);
        }
//        $questionModel = new QuestionModel();
//        $findQuestion = $questionModel->where("tagId = {$id}")->find();
//        if(!empty($findQuestion)){
//            $this->error("此标签被题库使用，无法删除",'',1);
//        }
        $model = new PaperModel();
        $delete = $model->delete($id);
        if ($delete) {
            $this->success('删除成功', '/Admin/Paper', 1);
        } else {
            $this->error('删除失败','',1);
        }
    }

    /**
     * 修改试卷
     */

    public function paperEdit(){
        $id = intval(I("get.id"));
        if (empty($id) || $id == 0) {
            $this->error("非法参数！",'',1);
        }
        $questionTagModel = new QuestionTagModel();
        $questionModel = new QuestionModel();
        $tagList = $questionTagModel->select();

        foreach($tagList as $k=>$v){
            $tagList[$k]['question'] = $questionModel->where(" `tagId` = {$v['id']}")->select();
        }
        $this->assign("tagList",$tagList);

        $model = new PaperModel();
        $data = $model->find($id);
        $this->assign("titles",$data['titles']);
        $this->assign("data",$data);
        $this->display();
    }

    public function modifyPaper(){
        $id = intval(I("post.id"));
        $name = I("post.name");
        $question_time = I("post.question_time");
        $titles = I("post.titles");
        $titles = implode(",",$titles);
        $titles = ",".$titles;

        if (empty($id) || $id == 0) {
            $this->error("非法参数！",'',1);
        }
        if (empty($name)) {
            $this->error("非法参数！",'',1);
        }
        if (empty($question_time) || $id == 0) {
            $this->error("考试时间至少1分钟","",1);
        }
        $model = new PaperModel();
        $data['name'] = $name;
        $data['question_time'] = $question_time;
        $data['titles'] = $titles;

        $saveTag = $model->where("id = ".$id."")->save($data);

        if($saveTag){
            $this->success('修改成功', '/Admin/Paper', 1);
        }else{
            $this->error('修改失败','',1);
        }
    }

}